/**
 * 项目名：化工企业安全生产信息化管理平台
 * 日期：    2020-07-26 17:45:28
 * Copyright (c) 2015- -版权所有
 */
package com.joysuch.wwyt.bp.controller;

import cn.zlg.common.springmvc.validate.constraints.NotNull;
import com.joysuch.wwyt.bp.bean.BpDeviceFacilityMaintainRecordEditBean;
import com.joysuch.wwyt.bp.bean.BpDeviceFacilityMaintainRecordListBean;
import com.joysuch.wwyt.bp.service.BpDeviceFacilityMaintainRecordService;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.exception.ApiBusinessException;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

import static com.joysuch.wwyt.bp.bean.BpDeviceFacilityMaintainRecordListBean.SearchBpDeviceFacilityMaintainRecord;

/**
 * 设备保养记录
 */
@RestController
@Api(tags = "设备设施管理-设备保养")
@RequestMapping("/bp/device-facility-maintain-record")
public class BpDeviceFacilityMaintainRecordController {

    private static final Logger logger = LoggerFactory.getLogger(BpDeviceFacilityMaintainRecordController.class);

    @Autowired
    private BpDeviceFacilityMaintainRecordService bpDeviceFacilityMaintainRecordService;


    @RequestMapping("add")
    public Object add(@Valid @RequestBody BpDeviceFacilityMaintainRecordEditBean entity) {
        try {
            bpDeviceFacilityMaintainRecordService.add(entity);
            return ResultBean.success("");
        } catch (ApiBusinessException ex) {
            return ResultBean.fail(ex.getCode(), ex.getMessage());
        }
    }


    @RequestMapping("update")
    public Object update(@Valid @RequestBody BpDeviceFacilityMaintainRecordEditBean entity) {
        if (entity.getId() == null) {
            return ResultBean.fail(102, "未指定主键");
        }
        bpDeviceFacilityMaintainRecordService.update(entity);
        return ResultBean.success("");
    }


    @RequestMapping("page")
    public Object page(Pageable page, SearchBpDeviceFacilityMaintainRecord condition) {
        Page<BpDeviceFacilityMaintainRecordListBean> data = bpDeviceFacilityMaintainRecordService.findByPage(page, condition);
        return ResultBean.pageData(data.getContent(), data.getTotalElements());
    }


    @RequestMapping("select-page")
    public Object selectPage(Pageable page, SearchBpDeviceFacilityMaintainRecord condition) {
        Page<BpDeviceFacilityMaintainRecordListBean> data = bpDeviceFacilityMaintainRecordService.findByPage(page, condition);
        return ResultBean.pageData(data.getContent(), data.getTotalElements());
    }

    @RequestMapping("delete")
    public Object delete(Long id) throws Exception {
        bpDeviceFacilityMaintainRecordService.delete(id);
        return ResultBean.success("");
    }

    @GetMapping("/deleteById")
    public ResultBean deleteById(Long recordId) {
        bpDeviceFacilityMaintainRecordService.deleteById(recordId);
        return ResultBean.success("");
    }


    @RequestMapping("get")
    public Object getById(@Valid @NotNull(field = "id") Long id) {
        return ResultBean.success(bpDeviceFacilityMaintainRecordService.getById(id));
    }
}

